﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ByteSite.Web.UI
{
    public partial class BasePage : System.Web.UI.Page
    {
        /// <summary>
        /// 返回当前类别名称
        /// </summary>
        /// <param name="categoryId">类别ID</param>
        /// <returns>String</returns>
        public string GetCategoryTitle(int categoryId, string defaultValue)
        {
            BLL.article_category bll = new BLL.article_category();
            if (bll.Exists(categoryId))
            {
                return bll.GetTitle(categoryId);
            }
            return defaultValue;
        }

        /// <summary>
        /// 返回类别一个实体类
        /// </summary>
        /// <param name="categoryId">类别ID</param>
        /// <returns>Model.category</returns>
        public Entitys.dt_article_category GetCategoryModel(int categoryId)
        {
            return new BLL.article_category().GetModel(categoryId);
        }

        /// <summary>
        /// 返回类别导航面包屑
        /// </summary>
        /// <param name="urlKey">URL重写Name</param>
        /// <param name="categoryId">类别ID</param>
        /// <returns>String</returns>
        public string GetCategoryMenu(string urlKey, int categoryId)
        {
            StringBuilder strTxt = new StringBuilder();
            if (categoryId > 0)
            {
                LoopChannelMenu(strTxt, urlKey, categoryId);
            }
            return strTxt.ToString();
        }

        /// <summary>
        /// 返回类别列表
        /// </summary>
        /// <param name="channelName">频道名称</param>
        /// <param name="parentId">父类别ID</param>
        /// <returns>DataTable</returns>
        public List<Entitys.dt_article_category> GetCategoryList(string channelName, int parentId)
        {
            return new BLL.article_category().GetList(parentId, channelName);
        }

        /// <summary>
        /// 返回指定类别下列表(一层)
        /// </summary>
        /// <param name="channelName">频道名称</param>
        /// <param name="parentId">父类别ID</param>
        /// <returns>DataTable</returns>
        public List<Entitys.dt_article_category> GetCategoryChildList(string channelName, int parentId)
        {
            return new BLL.article_category().GetChildList(0, parentId, channelName);
        }

        #region 私有方法===========================================
        /// <summary>
        /// 递归找到父节点
        /// </summary>
        private void LoopChannelMenu(StringBuilder strTxt, string urlKey, int categoryId)
        {
            BLL.article_category bll = new BLL.article_category();
            int parentId = bll.GetParentId(categoryId);
            if (parentId > 0)
            {
                this.LoopChannelMenu(strTxt, urlKey, parentId);
            }
            strTxt.Append("&nbsp;&gt;&nbsp;<a href=\"" + LinkUrl(urlKey, categoryId, 1) + "\">" + bll.GetTitle(categoryId) + "</a>");
        }
        #endregion
    }
}
